package com.gm.commonlib.base.baseui.baseListviewUI;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.gm.commonlib.R;
import com.gm.commonlib.base.baseBean.PageInfo;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

public abstract class BaseListActivity<
        DataType,
        NetworkResponse> extends BasePullToRefeshActivity<
        DataType,
        NetworkResponse> {
    public ListView pullToRefreshListView;
    public TextView tv_no_content;
    public RefreshLayout mRefreshLayout;
    protected BaseAdapter mBaseAdapter = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isEnterGetData = getIsEnterGetData();
        initPullToRefreshListView();
        mBaseAdapter = createTempBaseAdapter();
        pullToRefreshListView.setAdapter(mBaseAdapter);
        if (isEnterGetData) {
            requestPageData(1, true);
        }
    }

    public void initPullToRefreshListView() {
        setRefreshAndLoadEnable(true, true);
        setPullToRefreshListViewListener();
    }

    /**
     * 创建界面Item的适配器
     *
     * @return 适配器
     */
    protected abstract BaseAdapter createTempBaseAdapter();


    public void setPullToRefreshListViewListener() {
        mRefreshLayout.setOnRefreshListener(refreshlayout -> onRefreshView());
        mRefreshLayout.setOnLoadMoreListener(refreshlayout -> loadMore());
    }


    //获取数据完成，重置下拉控件状态
    public void resetPullToRefreshListView() {
        if (mPageInfo == null) {
            setRefreshAndLoadEnable(true, false);
            return;
        }
        if (isLastPagerData(mPageInfo) || mDatas.isEmpty()) {
            setRefreshAndLoadEnable(true, false);
        } else {
            setRefreshAndLoadEnable(true, true);
        }
    }

    protected boolean isLastPagerData(PageInfo pageInfo) {
        return pageInfo != null && pageInfo.getTotalPages() > 0 && (mCurrentPage == pageInfo.getTotalPages());
    }


    protected void setRefreshAndLoadEnable(boolean isPull, boolean isLoadMore) {
        mRefreshLayout.setEnableLoadMore(isLoadMore);
        mRefreshLayout.setEnableRefresh(isPull);
        mRefreshLayout.setEnableAutoLoadMore(isLoadMore);

    }

    @Override
    public void getDataOnCompleted() {
        super.getDataOnCompleted();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    /**
     * 提供默认的创建刷新界面的处理</br>
     *
     * @param parent 向parent设置滑动布局 内容
     */
    @SuppressLint("WrongViewCast")
    protected void onCreateContentBody(ViewGroup parent) {
        LayoutInflater.from(this).inflate(R.layout.base_list_pulltorefresh, parent);
        pullToRefreshListView = findViewById(R.id.listView);
        mRefreshLayout=findViewById(R.id.refreshLayout);
        tv_no_content = findViewById(R.id.tv_no_content);
    }


    protected void notifyListDataSetChanged() {
        if (mBaseAdapter != null) {
            mBaseAdapter.notifyDataSetChanged();
        }
        if (mDatas.isEmpty()) {
            tv_no_content.setVisibility(View.VISIBLE);
        } else {
            tv_no_content.setVisibility(View.GONE);
        }

    }

    public void setNoContentText(String text) {
        if (!TextUtils.isEmpty(text)) {
            tv_no_content.setText(text);
        }
    }

}
